package com.tanhua.backmanage.controller;


import com.tanhua.backmanage.service.UsersManageServiceQQ;
import com.tanhua.backmanage.vo.AllUsersVo;
import com.tanhua.backmanage.vo.UserInfoVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("manage")
@Slf4j
public class UsersManageControllerQQ {

    @Autowired
    private UsersManageServiceQQ usersManageServiceQQ;

    /**
     * 用户列表
     * @param page
     * @param pageSize
     * @param userId
     * @param nickName
     * @param city
     * @return
     */
    @GetMapping("users")
    public ResponseEntity<AllUsersVo> findAllUsers(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "id", required = false) Integer userId,
            @RequestParam(value = "nickname", required = false) String nickName,
            @RequestParam(value = "city", required = false) String city) {
        try {
            AllUsersVo allUsersVo = usersManageServiceQQ.findAllUsers(page, pageSize, userId, nickName, city);
            if (!CollectionUtils.isEmpty(allUsersVo.getItems())) {
                return ResponseEntity.ok(allUsersVo);
            }
        } catch (Exception e) {
            log.error("查询失败", e);
        }

        return ResponseEntity.status(500).build();
    }

    /**
     * 单个用户详细信息
     * @param userId
     * @return
     */
    @GetMapping("/users/{userID}")
    public ResponseEntity<UserInfoVo> queryUserInfo(@PathVariable("userID") Integer userId) {
        try {
            UserInfoVo userInfoVo = usersManageServiceQQ.queryUserInfo(Long.valueOf(userId));
            System.out.println(userInfoVo.toString());
            if (userInfoVo != null) {
                return ResponseEntity.ok(userInfoVo);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 用户冻结操作
     * @param param
     * @return
     */
    @PostMapping("/users/freeze")
    public ResponseEntity<String> freezeUser(@RequestBody Map<String, String> param) {

        try {
            String userId = param.get("userId");
            String freezingTime = param.get("freezingTime");
            String freezingRange = param.get("freezingRange");
            String reasonsForFreezing = param.get("reasonsForFreezing");
            String frozenRemarks = param.get("frozenRemarks");

            String result = usersManageServiceQQ.freezeUser(Integer.valueOf(userId), Integer.valueOf(freezingTime), Integer.valueOf(freezingRange), reasonsForFreezing, frozenRemarks);
            if (result != null) {
                return ResponseEntity.ok(result);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.status(500).build();
    }

    /**
     * 解冻
     * @param param
     * @return
     */
    @PostMapping("/users/unfreeze")
    public ResponseEntity<Boolean> unFreezeUser(@RequestBody Map<String, String> param) {
        try {
            String userId = param.get("userId");
            Boolean result = usersManageServiceQQ.unFreezeUser(Integer.valueOf(userId));
            if (result != null) {
                return ResponseEntity.ok(true);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return ResponseEntity.status(500).build();
    }
}
